<?php

/**
 * @author Sven-Ove Bjerkan
 * @copyright 2008
 */

class Config {	
	private static $instance;
	
	private $config = array();
	
	private function __autoLoad() {
		
	}
	
	/**
	 * Constructor.
	 * Connects to the database and fetches the configurations.
	 */
	private function __construct() {
		error_reporting(E_ALL | E_STRICT);	
		$this->fetchConfigFromDB();
		
		date_default_timezone_set($this->getConf('timezone'));
	}
	
	private function fetchConfigFromDB() {
		require_once('db.class.php');
		$con = DB::singleton()->getCon();
		
		if (!is_null($con)) {
			$qry = "SELECT * FROM settings";
			$res = mysqli_query($con, $qry);
			
			if ($res !== false) {
				$this->config = array();
				while ($row = mysqli_fetch_assoc($res)) {
					$this->config[$row['confName']] = $row['confValue'];	
				}
			}
		}
	}
	
	/**
	 * The singleton method.
	 * @return The instance of the config-class.
	 */
    public static function singleton() {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }
    
    /**
	 * Prevent users to clone the instance.
	 */
    public function __clone() {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
		
	/**
	 * Get configuration.
	 * @param String $key Name of the config-setting you want, or NULL if you want to fetch all.
	 * @param boolean $forceFetch true/false if you want to force a new fetch from the database
	 * @return Configuration-setting, or all config
	 */
	public function getConf($key = NULL, $forceFetch = false) {
		if ($forceFetch) $this->fetchConfigFromDB();
		
		// Fetch all configurations
		if (is_null($key)) return $this->config;
		// Fetch spesific configuration
		else if (isset($this->config[$key])) return $this->config[$key];
		// Return NULL if trying to fetch invalid config-name
		else return NULL;
	}
	
	/**
	 * Get all the discounts from setting
	 * @return An array with all the discounts with key 'limit' and 'dicountPercent'
	 */
	public function getDiscounts() {
		$discounts = array();
		foreach ($this->config as $key => $value) {
			if (strpos($key, 'adultNumLimit') !== false) {
				$tempArr = explode('_', $key);
				$discounts[$tempArr[sizeof($tempArr)-1]]['limit'] = $value;
			}
			else if (strpos($key, 'adultNumDiscountPercent') !== false) {
				$tempArr = explode('_', $key);
				$discounts[$tempArr[sizeof($tempArr)-1]]['discountPercent'] = $value; 
			}
		}
		return $discounts;
	}
	
	/**
	 * Update a config-setting
	 * @param String $name Name of the setting to update
	 * @param String $value Value of the setting
	 * @return boolean with status
	 */
	public function updateConf($name, $value) {
		$query = "UPDATE settings SET confValue=? WHERE confName=?";
		$query = DB::singleton()->getQuery($query, array('s', 's'), array($value, $name));
		
		return DB::singleton()->execUpdateQuery($query);
	}
	
		
	/**
	 * Add a new a config-setting
	 * @param String $name Name of the setting to add
	 * @param String $value Value of the setting
	 * @return boolean with status
	 */
	public function addConf($name, $value) {
		$query = "INSERT INTO settings (confName, confValue) VALUES (?,?)";
		$query = DB::singleton()->getQuery($query, array('s', 's'), array($name, $value));
		
		return DB::singleton()->execInsertQuery($query);
	}
	
		
	/**
	 * Delete a config-setting
	 * @param String $confName Name of the setting to delete
	 * @return boolean with status
	 */
	public function deleteConf($confName) {
		$query = "DELETE FROM settings WHERE confName=?";
		$query = DB::singleton()->getQuery($query, array('s'), array($confName));
		
		return DB::singleton()->execDeleteQuery($query);
	}
}
?>